package com.fenghongzhang.day014;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class MainActivity extends AppCompatActivity {
    private TextView text;
    private Button btn;
    private ImageView img;
    private ProgressBar bar;

    private static final String TAG = "MainActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        img = (ImageView) findViewById(R.id.img);
        text = (TextView) findViewById(R.id.text);
        btn = (Button) findViewById(R.id.btn);
        bar = (ProgressBar) findViewById(R.id.bar);



        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //下载->权限
//                new MyTask().execute("http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=20&page=1");
//                new MyTask2().execute("http://www.qubaobei.com/ios/cf/uploadfile/132/9/8289.jpg");
                new MyTask3().execute("http://www.qubaobei.com/ios/cf/uploadfile/132/9/8--89.jpg");
            }
        });

    }

    /**
     * 1,显示进度条
     * 2,更新进度 (int)
     *   进度怎么算?
     *   当前的进度 / 总大小  2 / 100 = 0.02 * 100 ->2
     *   发布进度
     *   接收进度
     * 3,进度条消失
     * 4,更新图片
     */
    class MyTask3 extends AsyncTask<String,Integer,Bitmap>{

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            bar.setVisibility(View.VISIBLE);//1
        }

        @Override
        protected void onProgressUpdate(Integer... values) {
            super.onProgressUpdate(values);
            //更新进度
            bar.setProgress(values[0]);
        }


        @Override
        protected void onPostExecute(Bitmap bitmap) {
            super.onPostExecute(bitmap);
            //进度条消失
            bar.setVisibility(View.GONE);
            img.setImageBitmap(bitmap);

        }

        @Override
        protected Bitmap doInBackground(String... strings) {
            try {
                Log.i(TAG, "doInBackground: ");
                URL url = new URL(strings[0]);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                if(connection.getResponseCode() == 200){
                    Log.i(TAG, "doInBackground: ");
                    InputStream inputStream = connection.getInputStream();
                    //当前进度
                    int cruuLen = 0;
                    //总进度
                    int contentLength = connection.getContentLength();
                    //内存流->作为文件的缓存
                    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                    byte[] bytes = new byte[1024];
                    int len = 0;
                    while ((len =  inputStream.read(bytes)) != -1){
                        //写入内存
                        byteArrayOutputStream.write(bytes,0,len);
                        cruuLen += len;
                        //进度值
                        int progress = (cruuLen * 100 / contentLength);
                        Log.i(TAG, "doInBackground: "+progress);
                        Thread.sleep(100);
                        //发布进度值
                        publishProgress(progress);
                    }
                    //取内存中的值
                    byte[] bytes1 = byteArrayOutputStream.toByteArray();
                    Bitmap bitmap = BitmapFactory.decodeByteArray(bytes1, 0, bytes1.length);

                    return bitmap;
                }

                return null;

            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return null;
        }
    }


































    //1,
    class MyTask extends AsyncTask<String,Void,String>{

        @Override
        protected String doInBackground(String... strings) {
            try {
                URL url = new URL(strings[0]);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();

                if(connection.getResponseCode() == 200){
                    InputStream inputStream = connection.getInputStream();
                    StringBuffer stringBuffer = new StringBuffer();
                    byte[] bytes = new byte[1024];
                    int len = 0;
                    while((len = inputStream.read(bytes)) != -1){
                        stringBuffer.append(new String(bytes,0,len));
                    }
                    String string = stringBuffer.toString();
                    return string;
                }

            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

        /**
         * doinBc 自动调用
         * @param s
         */
        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            text.setText(s);
        }

        @Override
        protected void onProgressUpdate(Void... values) {
            super.onProgressUpdate(values);
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            Log.i(TAG, "onPreExecute: 开始下载...");
        }
    }

    class MyTask2 extends AsyncTask<String,Void, Bitmap>{

        @Override
        protected Bitmap doInBackground(String... strings) {
            try {
                URL url = new URL(strings[0]);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();

                if(connection.getResponseCode() == 200){
                    InputStream inputStream = connection.getInputStream();
                    Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                    return bitmap;
                }

            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Bitmap bitmap) {
            super.onPostExecute(bitmap);
            img.setImageBitmap(bitmap);
        }
    }


}
